package com.itc.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.StringUtils;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.dispatch.domain.SysCacheData;
import com.itc.dispatch.service.SysCacheDataService;
import com.itc.dispatch.mapper.SysCacheDataMapper;
import org.springframework.stereotype.Service;

/**
* @author 91516
* @description 针对表【sys_cache_data】的数据库操作Service实现
* @createDate 2024-09-26 17:45:26
*/
@Service
public class SysCacheDataServiceImpl extends ServiceImpl<SysCacheDataMapper, SysCacheData>
    implements SysCacheDataService{

    @Override
    public R saveCache(SysCacheData dto) {
        if(StringUtils.isBlank(dto.getDataType())){
            return R.fail("数据类型不能为空");
        }
        Long userId = SecurityUtils.getUserId();

        SysCacheData cacheData = this.baseMapper.selectOne(
                new LambdaQueryWrapper<SysCacheData>()
                .eq(SysCacheData::getDataType,dto.getDataType())
                .eq(SysCacheData::getUserid,userId)
                .last("limit 1")
        );
        dto.setUserid(userId);
        if(cacheData == null){
            this.baseMapper.insert(dto);
        }else{
            cacheData.setCacheData(dto.getCacheData());
            this.baseMapper.updateById(cacheData);
        }

        return R.ok();
    }

    @Override
    public R getCurrentCache(SysCacheData dto) {
        if(StringUtils.isBlank(dto.getDataType())){
            return R.fail("数据类型不能为空");
        }
        Long userId = SecurityUtils.getUserId();
        SysCacheData cacheData = this.baseMapper.selectOne(
                new LambdaQueryWrapper<SysCacheData>()
                        .eq(SysCacheData::getDataType,dto.getDataType())
                        .eq(SysCacheData::getUserid,userId)
                        .last("limit 1")
        );
        return R.ok(cacheData);
    }
}




